package cn.virens.config;

import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.virens.web.components.beetl.BeetlGroupUtilConfigurationBean;
import cn.virens.web.components.beetl.format.ImageFormat;
import cn.virens.web.components.beetl.fun.FormCheckFunction;
import cn.virens.web.components.beetl.fun.FormSelectFunction;
import cn.virens.web.components.beetl.tag.menus.MenuTag;

@Configuration
public class SpringBeetlConfig {
	@Value("${web.cdn}")
	private String webCdn;

	@Value("${web.title}")
	private String webTitle;

	@Value("${web.footer}")
	private String webFooter;

	@Value("${web.keywords}")
	private String webKeywords;

	@Value("${web.description}")
	private String webDescription;

	@Bean
	public BeetlGroupUtilConfiguration beetlConfig() {
		BeetlGroupUtilConfigurationBean configuration = new BeetlGroupUtilConfigurationBean();

		configuration.putPropertie("DELIMITER_STATEMENT_START", "<tpl");
		configuration.putPropertie("DELIMITER_STATEMENT_END", "/>");
		configuration.registerFunction("selected", formSelectFunction());
		configuration.registerFunction("checked", formCheckFunction());
		configuration.registerFormat("image", imageFormat());
		configuration.registerTagFactory("menu", menuTag());

		configuration.putSharedVar("defAvatar", ImageFormat.DEF_AVATAR);
		configuration.putSharedVar("defImage", ImageFormat.DEF_IAMGE);

		configuration.putSharedVar("webDescription", webDescription);
		configuration.putSharedVar("webKeywords", webKeywords);
		configuration.putSharedVar("webFooter", webFooter);
		configuration.putSharedVar("webTitle", webTitle);
		configuration.putSharedVar("webCdn", webCdn);

		return configuration;
	}

	@Bean
	public MenuTag menuTag() {
		return new MenuTag();
	}

	@Bean
	public ImageFormat imageFormat() {
		return new ImageFormat();
	}

	@Bean
	public FormCheckFunction formCheckFunction() {
		return new FormCheckFunction();
	}

	@Bean
	public FormSelectFunction formSelectFunction() {
		return new FormSelectFunction();
	}
}
